package data;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import logic.Bcbs;

public class Dao {

	private static final String URL = "jdbc:mysql://localhost:3306/BCBSDatabase";
	private static final String USERNAME = "root";
	private static final String PASSWORD = "server123";

	private Connection connection = null;
	private PreparedStatement showAll = null;
	private PreparedStatement showBalance = null;
	private Statement statement = null;
	private ResultSet resultSet = null;
	


	public Dao() {
	DbConnection(URL, USERNAME, PASSWORD);
	}
	
	
	public void DbConnection( String url, String username, String password) {
		try {
			connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			statement = connection.createStatement(); 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		}
	}

	public void showAll() {
		try {
			showAll = connection.prepareStatement("SELECT * FROM users");
			//showAll = connection.prepareStatement("SELECT email, name, balance, isAdmin FROM users");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void showBalance(){
		try {
			showBalance = connection.prepareStatement("SELECT BALANCE FROM users WHERE email = '"+Bcbs.currentUser+"'");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public List<User> getUsers() {
		List<User> list = null;
		ResultSet resultSet = null;
		showAll();
		try {
			resultSet = showAll.executeQuery();
			list = new ArrayList<User>();
			while (resultSet.next()) {

				list.add(new User(resultSet.getString("email"),
						resultSet.getString("name"),
						resultSet.getString("password"),
						resultSet.getDouble("balance"),
						resultSet.getBoolean("Administrator")
						));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}

	public void close() {
		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public PreparedStatement getShowAll() {
		return showAll;
	}
	
	public PreparedStatement getShowBalance(){
		return showBalance;
	}
}
